package com.zs.main.filter;

import com.zs.common.utils.ServiceContext;
import org.slf4j.MDC;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
 * Copyright 2020 ZS Network Co., Ltd. All rights reserved.
 * 本文件仅限于正数网络技术有限公司内部传阅，禁止外泄以及用于其他的商业目的。
 * Created by yangning on 2021/7/15.
 */
@Component
public class GlobalFilter extends OncePerRequestFilter implements Ordered {


    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
         /*
            全局唯一流水
         */
        String sn = UUID.randomUUID().toString().replace("-", "");
        ServiceContext.setSN(sn);
        //
        MDC.put("sn", sn);
        filterChain.doFilter(httpServletRequest, httpServletResponse);
        MDC.clear();
        /*
            清除线程变量，防止数据重复
         */
        ServiceContext.clear();
    }

    @Override
    public int getOrder() {
        return -1;
    }
}
